package com.wj.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.github.pagehelper.PageInfo;
import com.wj.pojo.Bugall;
import com.wj.pojo.Tester;
import com.wj.service.BugAllService;
import com.wj.service.TesterService;
import com.wj.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试人员管理
 */
@Controller
@RequestMapping("/tester")
public class TesterController {

    @Autowired
    private TesterService testerService;

    @Autowired
    private BugAllService bugAllService;

    /**
     * 获取验证码
     * @param session
     * @param response
     * @throws IOException
     */
    @GetMapping(value = "/checkCode")
    public void checkCode(HttpSession session, HttpServletResponse response) throws IOException {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 5);
        String code = lineCaptcha.getCode();

        session.setAttribute("code",code);
        lineCaptcha.write(response.getOutputStream());
    }

    /**
     * 测试人员登录
     * @param tester
     * @param checkCode
     * @param session
     * @return
     */
    @PostMapping(value = "/login")
    @ResponseBody
    public Object login(Tester tester, String checkCode, HttpSession session){

        Map<String,Object> map = new HashMap<String, Object>();
        if (StringUtils.isEmpty(tester.getUsername())){
            map.put("type","error");
            map.put("msg","用户名不能为空");
            return map;
        }
        if (StringUtils.isEmpty(tester.getPassword())){
            map.put("type","error");
            map.put("msg","密码不能为空");
            return map;
        }
        if (StringUtils.isEmpty(checkCode)){
            map.put("type","error");
            map.put("msg","验证码不能为空");
            return map;
        }

        String code = (String) session.getAttribute("code");
        if (!checkCode.equalsIgnoreCase(code)){
            map.put("type","error");
            map.put("msg","验证码不正确");
            return map;
        }else {
            Tester t = testerService.login(tester);
            session.setAttribute("tester",t);
            map.put("type","success");
            map.put("msg","登录成功");
            return map;
        }
    }

    /**
     * 分页查询bug信息
     * @param gamename
     * @param state
     * @param degree
     * @param page
     * @param limit
     * @return
     */
    @GetMapping(value = "/list")
    @ResponseBody
    public Object list(String gamename,String state,String degree,int page,int limit) {
        Map<String,Object> map = new HashMap<String, Object>();
        if (!StringUtils.isEmpty(gamename)){
            map.put("gamename",gamename);
        }
        if (!StringUtils.isEmpty(state)){
            map.put("state",state);
        }
        if (!StringUtils.isEmpty(degree)){
            map.put("degree",degree);
        }
        PageInfo<Bugall> pageInfo = bugAllService.findByPage(page,limit,map);
        return JsonData.buildSuccess(pageInfo);
    }

    /**
     * 新增bug信息
     * @param bugall
     * @return
     */
    @PostMapping(value = "/add")
    @ResponseBody
    public Object add(Bugall bugall){
        if (bugall!=null){
            bugAllService.add(bugall);
            return JsonData.buildSuccess("rs","添加成功");
        }else {
            return JsonData.buildError("添加失败");
        }
    }

    /**
     * 修改一条bug信息
     * @param bugall
     * @return
     */
    @PostMapping(value = "/upd")
    @ResponseBody
    public Object upd(Bugall bugall){
        int i = bugAllService.upd(bugall);
        if (i>0){
            return JsonData.buildSuccess("rs","修改成功");
        }
        return JsonData.buildError("修改失败");
    }









}
